/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package routefinder.implementation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import routefinder.City;
import routefinder.Link;

/**
 *
 * @author Lucas
 */
public class RoutesMapCities implements IRoutesMap {
    Map<City,City> cities = new HashMap();


    public Map<City, City> getCities() {
        return (Map<City, City>) ((HashMap)cities).clone();
    }
    
    /**
     * Get the list of cities that can be reached from the given city.
     */
    public List<Link> getDestinations(City city) {
        final City existingCity = cities.get(city);
        List<Link> destinations = new ArrayList<Link>();
        if(existingCity != null){
            destinations = existingCity.getNeighbours();
        }
        return destinations;
    }

    /**
     * Enter a new edge in the graph.
     */
    public void addDirectRoute(City origin, City destination, int distance) {
        final City previousReference = cities.get(origin);
        if (previousReference == null) {
            // city still doesn't exist on the cities map
            origin.addNeighbour(destination, distance);
            cities.put(origin, origin);
        } else {
            // updates the existing city reference on the cities map
            previousReference.addNeighbour(destination, distance);
            cities.put(previousReference, previousReference);
        }
    }

}
